/**
 * Created by shitao on 15/10/17.
 */

(function () {
    // 创建service对象
    angular.module('my-tabs.services')
        .factory('appService', appService);
    // 申明依赖
    appService.$inject = ['appApi', 'sessionService'];

    function appService(appApi, session) {

        var mines = [],
            frames = {};

        return {

            load: function () {
                var promise;

                if (session.isSignIn()) {
                    promise = appApi.getApps(session.getSignInUser().id);
                } else {
                    promise = appApi.getDefaultApps();
                }
                return promise.success(function (items) {
                    mines.length = 0;
                    _.each(items, function(item){
                        mines.push(item);
                    })
                });
            },

            all: function(){
                return mines;
            },

            get: function (appId) {
                for (var i = 0; i < mines.length; i++) {
                    if (mines[i].id === parseInt(appId)) {
                        return mines[i];
                    }
                }
                return null;
            },

            getFrame: function(frameId){
                return frames[frameId];
            },

            putFrame: function(frame){
                if(!frames.hasOwnProperty(frame.id)){
                    frames[frame.id] = frame;
                }
            }

        };
    };
})();
